package com.wangyi.system.controller;

import com.wangyi.basic.util.AjaxResult;
import com.wangyi.basic.util.PageList;
import com.wangyi.system.domain.Dictionarydetail;
import com.wangyi.system.domain.Dictionarytype;
import com.wangyi.system.query.DictionarytypeQuery;
import com.wangyi.system.service.IDictionarydetailService;
import com.wangyi.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
@Api(value = "字典类型接口",description = "字典类型接口")
public class DictionarytypeController {

    @Autowired
    private IDictionarytypeService dictionarytypeService;
    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/dictionarytype/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id")Long id) {
        return dictionarytypeService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/dictionarytype + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Dictionarytype> findAll() {
        return dictionarytypeService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/dictionarytype + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query) {
        return dictionarytypeService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/dictionarytype/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id) {
        // 删除数据字典类型会同时删除数据字典明细
        try {
            List<Dictionarydetail> list = dictionarydetailService.findAll();
            for(Dictionarydetail de: list) {
                if (de.getTypes_id() == id) {
                    Long id1 = de.getId();
                    dictionarydetailService.del(id1);
                }
            }

            dictionarytypeService.del(id);
            return new AjaxResult(); // true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param dictionarytype
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/dictionarytype + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype) {
        try {
            if(dictionarytype.getId() == null){ // 添加
                dictionarytypeService.add(dictionarytype);
            }else{
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    // 前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            List<Dictionarydetail> list = dictionarydetailService.findAll();
            for(Dictionarydetail sy: list) {
                for(Long id: ids) {
                    if (sy.getTypes_id() == id) {
                        Long id1 = sy.getId();
                        dictionarydetailService.del(id1);
                    }
                }
            }
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult(); // true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}